package org.zxp.designpattern.singleton;

import org.junit.Test;

/**
 * @program: effectiveJava
 * @description:
 * @author: X-Pacific zhang
 * @create: 2019-08-21 21:14
 **/
public class TestSingleton {

    /**
     * 测试 饿汉 线程安全
     */
    @Test
    public void testSingleton1(){
        Singleton1 singleton1 = Singleton1.getInstance();
        Singleton1 singleton2 = Singleton1.getInstance();
        System.out.println(singleton1);//1586270964
        System.out.println(singleton2);//1586270964
    }

    /**
     * 测试 懒汉 线程不安全
     */
    @Test
    public void testSingleton2(){
        Singleton2 singleton1 = Singleton2.getInstance();
        Singleton2 singleton2 = Singleton2.getInstance();
        //非并发没问题
        System.out.println(singleton1);
        System.out.println(singleton2);
    }

    /**
     * 测试 懒汉 线程安全（低性能）
     */
    @Test
    public void testSingleton3(){
        Singleton3 singleton1 = Singleton3.getInstance();
        Singleton3 singleton2 = Singleton3.getInstance();
        System.out.println(singleton1);
        System.out.println(singleton2);
    }

    /**
     * 测试 懒汉 线程安全（高性能）double check
     */
    @Test
    public void testSingleton4(){
        Singleton4 singleton1 = Singleton4.getInstance();
        Singleton4 singleton2 = Singleton4.getInstance();
        System.out.println(singleton1);
        System.out.println(singleton2);
    }

    /**
     * 测试 懒汉 静态内部类
     */
    @Test
    public void testSingleton5(){
        Singleton5 singleton1 = Singleton5.getInstance();
        Singleton5 singleton2 = Singleton5.getInstance();
        System.out.println(singleton1);
        System.out.println(singleton2);
    }

    /**
     * 测试 饿汉 枚举
     */
    @Test
    public void testSingleton6(){
        Singleton6 singleton1 = Singleton6.getInstance();
        Singleton6 singleton2 = Singleton6.getInstance();
        singleton1.print();
        singleton2.print();
    }
}
